package com.xmy.cultivate.mapper;

import com.xmy.cultivate.entity.LklMerchantSet;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.Merchant;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 拉卡拉商户设置 Mapper 接口
 * </p>
 *
 * @author hpiggy
 * @since 2025-03-22
 */
public interface LklMerchantSetMapper extends BaseMapper<LklMerchantSet> {


    @Select({"<script>",
            "SELECT ",
            "s.school_id,",
            "o.name AS school_name",
//            "s.name AS name",
            "from",
            "lkl_merchant_set m",
            "JOIN",
            "staff s ON m.teacher_id = s.id ",
            "JOIN",
            "organization o ON s.school_id = o.id",
            "WHERE",
            "m.status = 1 ",
            " GROUP BY s.school_id",
            "</script>"})
    public List<Merchant> getSchool();

    @Select("SELECT lms.* FROM lkl_merchant_set lms LEFT JOIN staff s ON s.id = lms.teacher_id WHERE lms.`status` = 1 AND s.school_id = #{schoolId}")
    public List<LklMerchantSet> getMerchant(@Param("schoolId") Long schoolId);



    @Select({"<script>",
            "SELECT ",
            "id,school_id,",
            "getSchoolName(school_id) AS schoolName",
            "from",
            "staff",
            "where",
            "`status` = 1 ",
//            "AND is_teacher = 1 ",
            "AND deleted = 0",
            "GROUP BY",
            "school_id ",
            "</script>"})
    public List<Merchant> getMerchantSchool();



    @Select({"<script>",
            "SELECT ",
            "t2.merchant_name ",
            "FROM",
            "lkl_merchant_student t1",
            "JOIN lkl_merchant_set t2 ON t1.merchant_set_id = t2.id ",
            "WHERE",
            "t1.year_part = ${yearPart}",
            "AND t1.quarter_num = ${quarterNum}",
            "AND t1.subjects_id = ${subjectsId}",
            "AND t1.student_id = ${studentId}",
            "ORDER BY",
            "t1.created_at DESC",
            "LIMIT 1",
            "</script>"})
    public String getMerchantName(@Param("yearPart") String yearPart,@Param("quarterNum") String quarterNum,@Param("subjectsId") Long subjectsId,@Param("studentId") Long studentId);



    @Select({"<script>",
            "SELECT ",
            "t2.id,",
            "t2.teacher_id,",
            "t2.merchant_no,",
            "t2.merchant_name",
            "FROM",
            "lkl_merchant_student t1",
            "JOIN lkl_merchant_set t2 ON t1.merchant_set_id = t2.id ",
            "WHERE 1=1",
            "AND t2.status = 1",
            "<if test='yearPart != null and yearPart != &apos;&apos;'>",
            "AND t1.year_part = ${yearPart}",
            "</if>",
            "<if test='quarterNum != null and quarterNum != &apos;&apos;'>",
            "AND t1.quarter_num = ${quarterNum}",
            "</if>",
            "<if test='subjectsId != null and subjectsId != &apos;&apos;'>",
            "AND t1.subjects_id = ${subjectsId}",
            "</if>",
            "<if test='studentId != null and studentId != &apos;&apos;'>",
            "AND t1.student_id = ${studentId}",
            "</if>",

            "<if test='schoolId != null and schoolId != &apos;&apos;'>",
            "AND t1.school_id = ${schoolId}",
            "</if>",
            "ORDER BY",
            "t1.created_at DESC",
            "LIMIT 1",
            "</script>"})
    public LklMerchantSet afterSwitchMerchantNo(@Param("yearPart") String yearPart,
                                        @Param("quarterNum") String quarterNum,
                                        @Param("subjectsId") Long subjectsId,
                                        @Param("studentId") Long studentId,
                                        @Param("schoolId") Long schoolId);


    @Select({ "<script>",
            "SELECT",
            "t03.id,",
            "t03.teacher_id,",
            "t03.merchant_no,",
            "t03.merchant_name",
            "FROM",
            "lkl_merchant_teacher t01",
            "LEFT JOIN lkl_merchant t02 ON t02.id = t01.merchant_id AND t02.status = 1",
            "LEFT JOIN lkl_merchant_set t03 ON t03.id = t02.merchant_set_id AND t03.status = 1",
            "WHERE 1=1",
            "AND t01.deleted = 0",
            "<if test='teacherId != null and teacherId != &apos;&apos;'>",
                "AND t01.teacher_id = #{teacherId}",
            "</if>",
            "<if test='yearPart != null and yearPart != &apos;&apos;'>",
                "AND t02.year_part = ${yearPart}",
            "</if>",
            "<if test='quarterNum != null and quarterNum != &apos;&apos;'>",
                "AND t02.quarter_num = ${quarterNum}",
            "</if>",
            "<if test='schoolId != null and schoolId != &apos;&apos;'>",
                "AND t02.school_id = ${schoolId}",
            "</if>",
            "LIMIT 1",
            "</script>" })
    public LklMerchantSet beforeSwitchMerchantNo(
            @Param("yearPart") String yearPart,
            @Param("quarterNum") String quarterNum,
            @Param("teacherId") Long teacherId,
            @Param("schoolId") Long schoolId);


    @Select({"<script>",
            "SELECT",
            "t2.id,",
            "t2.teacher_id,",
            "t2.merchant_no,",
            "t2.merchant_name",
            "FROM",
            "lkl_merchant t1",
            "LEFT JOIN lkl_merchant_set t2 ON t1.merchant_set_id = t2.id",
            "WHERE 1=1",
            "AND t1.status = 1",
            "AND t2.status = 1",
            "AND t1.pid = 0",
            "<if test='yearPart != null and yearPart != &apos;&apos;'>",
            "AND t1.year_part = ${yearPart}",
            "</if>",
            "<if test='quarterNum != null and quarterNum != &apos;&apos;'>",
            "AND t1.quarter_num = ${quarterNum}",
            "</if>",
            "AND t1.school_id = ${schoolId}",
            "LIMIT 1",
            "</script>"})
    public LklMerchantSet getMerchantNoBySchoolId(
            @Param("yearPart") String yearPart,
            @Param("quarterNum") String quarterNum,
            @Param("schoolId") Long schoolId);
}
